package com.springboot.common.helper;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * 请求响应辅助工具
 */
public class RequestHelper {

    public static HttpServletRequest getRequest() {
        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return Optional.ofNullable(requestAttributes.getRequest()).get();
    }


    public static HttpServletResponse getResponse() {
        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return Optional.ofNullable(requestAttributes.getResponse()).get();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }


    public static String getRequestURI() {
        return getRequest().getRequestURI();
    }
}
